const express = require('express')
const app = express()
app.use(express.urlencoded({extended:false}))
app.use(express.json())
const port = 3000

const configs=require('./config')

let mysql=require('mysql')
let connection = mysql.createConnection(configs.mysql)
connection.connect()

app.get('/get',(req,res)=>{
    let sql='select * from Book'
    if(req.query.BookName)
    {
        sql=`select * from Book where BookName like'%${req.query.BookName}%'`
    }
    connection.query(sql,(err,rows)=>{
        if(!err){
            res.send(rows)
        }
        else{
            res.send(err)
        }
    })
})

app.post('/add',(req,res)=>{
    let sql=`insert into Book(BookName,Author,TypeName,Remarks) 
values('${req.body.BookName}','${req.body.Author}','${req.body.TypeName}','${req.body.Remarks}')`
    connection.query(sql,(err,rows)=>{
        if(!err){
            res.send('cz sss')
        }
        else{
            res.send(err)
        }
    })
})

app.post('/edit',(req,res)=>{
    let sql=`update Book set BookName='${req.body.BookName}',
Author='${req.body.Author}',TypeName='${req.body.TypeName}',
Remarks='${req.body.Remarks}' where Id=${req.body.Id}`
    connection.query(sql,(err,rows)=>{
        if(!err){
            res.send('cz sss')
        }
        else{
            res.send(err)
        }
    })
})

app.delete('/del',(req,res)=>{
    let sql=`delete from Book where Id=${req.query.Id}`
    connection.query(sql,(err,rows)=>{
        if(!err){
            res.send('cz sss')
        }
        else{
            res.send(err)
        }
    })
})

app.get('/',(req,res)=>{
    res.send('suss')
    connection.end()
})

app.listen(port,()=>{
    console.log(`listen on port ${port}`)
})